package dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Classroom;
import utils.JDBCUtils;
import utils.SystemEnum;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ClassroomDaoImpl implements ClassroomDao{

    private Log log = LogFactory.getLog(getClass());
    @Override
    public ArrayList<Classroom> findAll() {
        log.info(SystemEnum.BEGIN);
        ArrayList<Classroom> list = new ArrayList<Classroom>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM classroom ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                Classroom classroom = new Classroom(rs.getInt(1),rs.getString(2));
                list.add(classroom);
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return list;
    }

    @Override
    public Classroom findById(Integer classId) {
        log.info(SystemEnum.BEGIN);
        Classroom classroom = new Classroom();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM classroom WHERE classId = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,classId);
            rs = statement.executeQuery();
            while(rs.next()){
                classroom.setClassId(rs.getInt(1));
                classroom.setClassName(rs.getString(2));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return classroom;
    }

    @Override
    public Classroom findByName(String name) {
        log.info(SystemEnum.BEGIN);
        Classroom classroom = new Classroom();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM classroom WHERE className = ?";
            statement = con.prepareStatement(sql);
            statement.setString(1,name);
            rs = statement.executeQuery();
            while(rs.next()){
                classroom.setClassId(rs.getInt(1));
                classroom.setClassName(rs.getString(2));
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return classroom;
    }

    @Override
    public boolean findByIdReturnBoolean(Integer classId) {
        log.info(SystemEnum.BEGIN);
        boolean tag = false;
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM classroom WHERE classId = ? ";
            statement = con.prepareStatement(sql);
            statement.setInt(1, classId);
            rs = statement.executeQuery();
            while(rs.next()){
                tag=true;
                break;
            }
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE+"\t"+SystemEnum.ERROR_MATCH);
        }finally {
            JDBCUtils.close(con,statement,rs);
            log.info(SystemEnum.END);
        }
        return tag;
    }

    @Override
    public int insert(Classroom classroom) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "INSERT INTO classroom VALUES (?,?) ";
            statement = con.prepareStatement(sql);
            statement.setInt(1, classroom.getClassId());
            statement.setString(2, classroom.getClassName());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_REPEAT+"\t"+SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            log.info(SystemEnum.END);
            JDBCUtils.close(con, statement);
        }
        return result;
    }

    @Override
    public int update(Classroom classroom) {
        log.info(SystemEnum.BEGIN);
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE classroom SET className =? WHERE classId = ?";
            statement = con.prepareStatement(sql);
            statement.setString(1,classroom.getClassName());
            statement.setInt(2,classroom.getClassId());
            result = statement.executeUpdate();
            log.info(SystemEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.info(SystemEnum.ERROR_INEFFECTIVE);
        }finally {
            JDBCUtils.close(con,statement);
            log.info(SystemEnum.END);
        }
        return result;
    }

/*    @Override
    public int delete(Classroom classroom) {
        return 0;
    }*/
}
